<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :where="defaultWhere" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
      >
        <template #toolbar="{}">
          <el-button type="success" @click="exportExcel" :icon="DocumentCopy">
            导出表格
          </el-button>
        </template>
        <template #realname="{ row }">
          <span class="pointer blue" @click="showStudentDetail(row)">
            {{ row.realname }}
          </span>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 回访 -->
    <judge v-model="showJudge" :job="current" type="judge" @done="reload" />
    <!-- 学生详情 -->
    <detail v-model="showDetail" :student-id="currentId" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './search.vue';
  import Judge from '@/views/company/apply/components/pay.vue';
  import Detail from '@/views/student/components/detail.vue';
  import { pageJobSalary, exportJobSalary } from '@/api/money';
  import { DocumentCopy } from '@element-plus/icons-vue';
  import { EleMessage } from 'ele-admin-plus/es';
  /** 表格实例 */
  const tableRef = ref(null);

  const props = defineProps({
    defaultWhere: {
      type: Object,
      default: () => {}
    },
    action: {
      type: String,
      default: 'list'
    }
  });

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'site',
      label: '城市',
      align: 'center',
      width: 80
    },
    {
      prop: 'belongs_date',
      label: '所属月份',
      align: 'center',
      width: 85
    },
    {
      prop: 'code',
      label: '工作编号',
      align: 'center',
      width: 100
    },
    {
      prop: 'company_name',
      label: '企业名称',
      width: 230
    },
    {
      label: '工作名称',
      prop: 'title',
      minWidth: 150
    },
    {
      prop: 'work_date',
      label: '工作日期',
      minWidth: 130
    },
    {
      slot: 'realname',
      label: '姓名',
      width: 80,
      align: 'center'
    },
    {
      prop: 'tel',
      label: '手机',
      width: 115
    },
    {
      prop: 'identifier',
      label: '身份证',
      width: 100
    },
    {
      prop: 'working_time',
      label: '工时',
      width: 90,
      align: 'center'
    },

    {
      prop: 'salary',
      label: '单价',
      width: 90,
      align: 'center'
    },
    {
      prop: 'amount',
      label: '金额',
      width: 90,
      align: 'center'
    },
    {
      prop: 'judge_user',
      label: '审核人',
      width: 100,
      align: 'center'
    },
    {
      prop: 'judge_time',
      label: '审核时间',
      width: 150,
      align: 'center'
    },
    {
      prop: 'create_user',
      label: '提交人',
      width: 100,
      align: 'center'
    },
    {
      prop: 'create_time',
      label: '提交时间',
      width: 150,
      align: 'center'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 当前编辑数据 */
  const currentId = ref(null);

  /** 是否显示编辑弹窗 */
  const showJudge = ref(false);

  /** 是否显示编辑弹窗 */
  const showDetail = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageJobSalary({
      ...where,
      ...orders,
      page,
      limit,
      wating_pay: 1
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 学生详情 */
  const showStudentDetail = (row) => {
    currentId.value = row.student_id ?? 0;
    showDetail.value = true;
  };

  /** 导出表格 */
  const exportExcel = () => {
    const loading = EleMessage.loading('请求中..');
    tableRef.value?.fetch(({ where }) => {
      // 方法的参数与数据源的完全一致
      // 请求查询全部接口
      console.log(where);
      var requestData = where;
      requestData.limit = 99999999;
      requestData.page = 1;

      exportJobSalary(requestData)
        .then((res) => {
          loading.close();
          window.open(res);
        })
        .catch(() => {
          loading.close();
        });
    });
  };
</script>

<script>
  export default {
    name: 'SystemUser'
  };
</script>
